const mysql=require("mysql")
require("dotenv").config()
const pool=mysql.createPool({
    host:process.env.DB_HOST,
    user:process.env.DB_USER,
    password:process.env.DB_PWD,
    port:process.env.DB_PORT,
    database:process.env.DB_DATABASE
})

const exec=(sql,params)=>{
    return new Promise((resolve,reject)=>{
        pool.getConnection((err,conn)=>{
            if(err){
                console.log("连接数据库失败"+err.message);
                reject(false)
                return
            }
            conn.query(sql,params,(err,result)=>{
                if(err){
                    console.log("执行sql语句失败"+err.message);
                    reject(false)
                    return
                }
                resolve(result)
            })
            conn.release()
        })
    })
}

module.exports={exec}